﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DynaPrecision.Numerical.BoundaryCondition
{
	public class NeumannBoundaryProcessor<T> : ICartesianGridBoundaryHolder<T>, ILinearSystemGenerator<T>
	{

		#region ICartesianGridBoundaryHolder<T> Members

		public Mesh.ICartesianMeshData<T> Mesh
		{
			get;
			set;
		}

		#endregion

		#region ILinearSystemGenerator<T> Members

		public void ProcessLeftMatrix<TLeftArray>(ref TLeftArray leftMatrixStorage) where TLeftArray : LinearSystem.IMultiDimensionalArray<T>
		{
			int length0 = Mesh.GetLength(0);
			int length1 = Mesh.GetLength(1);
			for (int i = 1; i < length0 - 1; i++)
			{
				int currentI = i;
				leftMatrixStorage[currentI, currentI] = (dynamic)1;
				leftMatrixStorage[currentI, currentI + length0] = (dynamic)(-1);
				currentI = (length1 - 1) * length0 + i;
				leftMatrixStorage[currentI, currentI] = (dynamic)1;
				leftMatrixStorage[currentI, currentI - length0] = (dynamic)(-1);
			}
			for (int j = 1; j < length1 - 1; j++)
			{
				int currentI = j * length0;
				leftMatrixStorage[currentI, currentI] = (dynamic)1;
				leftMatrixStorage[currentI, currentI + 1] = (dynamic)(-1);
				currentI = j * length0 + length0 - 1;
				leftMatrixStorage[currentI, currentI] = (dynamic)1;
				leftMatrixStorage[currentI, currentI - 1] = (dynamic)(-1);
			}
			leftMatrixStorage[0, 0] = (dynamic)1;
			leftMatrixStorage[length0 - 1, length0 - 1] = (dynamic)1;
			leftMatrixStorage[length1 * length0 - length0, length1 * length0 - length0] = (dynamic)1;
			leftMatrixStorage[length1 * length0 - 1, length1 * length0 - 1] = (dynamic)1;
			leftMatrixStorage[length0, length0 + 1] = (dynamic)(0);
		}

		public void ProcessRightMatrix<TRightArray>(ref TRightArray rightMatrix) where TRightArray : LinearSystem.IMultiDimensionalArray<T>
		{
			return;
		}

		#endregion
	}
}
